﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Drawing;

namespace CarFrameWork.Common
{
    class ComponentDeal
    {
        //为下拉框添加一空行
        public static DataTable cmbNewBlank(DataTable dtOld, string fieldName)
        {
            DataTable dtNew = new DataTable();
            dtNew.Columns.Add(fieldName);
            dtNew.Rows.Add(dtNew.NewRow());

            DataRow dr;
            for (int i = 0; i < dtOld.Rows.Count; i++)
            {
                dr = dtNew.NewRow();
                dr[0] = dtOld.Rows[i][0].ToString();
                dtNew.Rows.Add(dr);
            }

            return dtNew;
        }

        public static Boolean checkGroupBoxData(System.Windows.Forms.Control control, string[,] groupBoxArray)
        {
            for (int i = 0; i < groupBoxArray.Length / 2; i++)
            {
                TextBox tb = (TextBox)findTextBox(control, groupBoxArray[i, 1]);
                try
                {
                    double.Parse(tb.Text);
                }
                catch
                {
                    MsgBox.Show("\"" + groupBoxArray[i, 0] + "\"数据不合法！");
                    tb.Focus();
                    return false;
                }
            }
            return true;
        }

        public static System.Windows.Forms.Control findTextBox(System.Windows.Forms.Control control, string controlName)
        {
            foreach (Control c1 in control.Controls)
            {
                if (c1.Name == controlName)
                {
                    return c1;
                }
            }
            return null;
        }

        public static void insertOrUpdateDataBase(int iOperatorType, string strInsertSql, string strUpdateSql)
        {
            string strSql = "";
            if (iOperatorType == 0)
            {
                //插入操作
                if (MsgBox.Show("确定保存数据？", "YESNO").CompareTo("YES") == 0)
                {
                    strSql = strInsertSql;
                }
                else
                {
                    return;
                }
            }
            else
            {
                //更新操作
                if (MsgBox.Show("已有此数据，确定修改该数据？", "YESNO").CompareTo("YES") == 0)
                {
                    strSql = strUpdateSql;
                }
                else
                {
                    return;
                }

            }
            string strTmp = iOperatorType == 0 ? "插入" : "更新";
            if (SqlWork.ExecuteSql(strSql) == true)
            {
                MsgBox.Show("数据" + strTmp + "成功！");
            }
            else
            {
                MsgBox.Show("数据" + strTmp + "失败！");
            }
        }
    }
}
